package com.linkage.bss.crm.packagemgr.bmo.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.linkage.bss.commons.dao.Page;
import com.linkage.bss.crm.packagemgr.bmo.IPackageStatusBMO;
import com.linkage.bss.crm.packagemgr.dao.IAreaDAO;
import com.linkage.bss.crm.packagemgr.dao.IPackageManagerDAO;
import com.linkage.bss.crm.packagemgr.dao.IPackageStatusDAO;
import com.linkage.bss.crm.packagemgr.model.Area;
import com.linkage.bss.crm.packagemgr.model.Channel;
import com.linkage.bss.crm.packagemgr.util.Constant;

/**
 *
 *包状态管理
 *
 * @author  Administrator
 * @version  [版本号, 2013-5-10]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class PackageStatusBMOImpl implements IPackageStatusBMO {

	IPackageManagerDAO packageManagerDAO = null;

	IPackageStatusDAO packageStatusDAO = null;

	private IAreaDAO areaDAO;

	public IAreaDAO getAreaDAO() {
		return areaDAO;
	}

	public void setAreaDAO(IAreaDAO areaDAO) {
		this.areaDAO = areaDAO;
	}

	public IPackageStatusDAO getPackageStatusDAO() {
		return packageStatusDAO;
	}

	public void setPackageStatusDAO(IPackageStatusDAO packageStatusDAO) {
		this.packageStatusDAO = packageStatusDAO;
	}

	public IPackageManagerDAO getPackageManagerDAO() {
		return packageManagerDAO;
	}

	public void setPackageManagerDAO(IPackageManagerDAO packageManagerDAO) {
		this.packageManagerDAO = packageManagerDAO;
	}

	public void savePackage2Area(Long packageID, Integer areaID) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("packageId", packageID);
		map.put("areaId", areaID);
		getPackageStatusDAO().savePackage2Area(map);

	}

	public void savePackage2Channel(Long packageID, Integer channelID) {

		Channel channel = getPackageStatusDAO().getChannelByID(channelID);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("packageId", packageID);
		map.put("channelId", channel.getChannelId());
		map.put("startDt", channel.getStartDt());
		map.put("endDt", channel.getEndDt());

		getPackageStatusDAO().savePackage2Channel(map);

	}

	public Boolean publishPackage(Long packageID, List<Integer> areaList, List<Integer> channelList, Long staffID) {

		//刪除包原有的發佈地區与渠道。
		getPackageStatusDAO().delPackage2Area(packageID);
		getPackageStatusDAO().delPackage2Channel(packageID);

		//保存包活动发布地区
		for (Integer areaId : areaList) {
			this.savePackage2Area(packageID, areaId);
		}
		//保存促销活动发布渠道
		for (Integer channelId : channelList) {
			this.savePackage2Channel(packageID, channelId);
		}
		// 更新状态
		getPackageStatusDAO().updatePackageStatus(packageID, Constant.STATUS_EFFECTIVE);
		getPackageStatusDAO().insertOPLog(packageID, staffID, Constant.STATUS_EFFECTIVE);
		return true;
	}

	public void startUsePackage(Long packageId, Long staffID) {
		//更改状态为生效
		getPackageStatusDAO().updatePackageStatus(packageId, Constant.STATUS_EFFECTIVE);
		//从失效变成生效
		getPackageStatusDAO().insertOPLog(packageId, staffID, Constant.STATUS_NO_EFFECTIVE);

	}

	public void stopUsePackage(Long packageId, Long staffID) {
		//更改状态为失效
		getPackageStatusDAO().updatePackageStatus(packageId, Constant.STATUS_NO_EFFECTIVE);
		//从生效变成失效
		getPackageStatusDAO().insertOPLog(packageId, staffID, Constant.STATUS_EFFECTIVE);

	}

	public List<Channel> getChannelByName(Map map, Page page) {

		return getPackageStatusDAO().getChannelByName(map, page);
	}

	public int getChannelByNameCount(Map map) {

		return getPackageStatusDAO().getChannelByNameCount(map);
	}

	public List<Area> getAreaByName(String name, Integer areaId, Page page) {

		return getAreaDAO().getAreaByName(name, areaId, page);
	}

	public int getAreaCountByName(String name, Integer areaId) {
		return getAreaDAO().getAreaCountByName(name, areaId);
	}

	public List<Area> queryPackage2Area(Long pakcageId) {
		return getAreaDAO().queryPackage2Area(pakcageId);

	}

	public List<Channel> queryPackage2Channel(Long pakcageId) {
		return getPackageStatusDAO().queryPackage2Channel(pakcageId);
	}

}
